﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using KeMei.DBUtility;//Please add references
namespace KeMei.DAL
{
    /// <summary>
    /// 数据访问类:ProductAndCase
    /// </summary>
    public partial class ProductAndCase
    {
        public ProductAndCase()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ProductAndCase");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            return OledbHelper.Exists(strSql.ToString(), parameters);
        }

        public bool ExistsNum(int ID, int num, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ProductAndCase ");
            strSql.Append(" where Taxis=@num and ID<>@ID ");

            OleDbParameter[] parameters = {
					new OleDbParameter("@num", OleDbType.Integer,4),
                    new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = num;
            parameters[1].Value = ID;

            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(strWhere);
            }
            return OledbHelper.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 设置排序
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="isUp">是否上移 上移：true,下移：false</param>
        /// <returns></returns>
        public bool UpdateTaxis(int ID, int num)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update ProductAndCase set Taxis=@num  ");


            strSql.Append(" where ID=@ID");

            OleDbParameter[] parameters = {
                    new OleDbParameter("@num",OleDbType.Integer,4),
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = num;
            parameters[1].Value = ID;

            int rows = OledbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(KeMei.Model.ProductAndCase model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ProductAndCase(");
            strSql.Append("lmID,TName,TContent,RecordDate,PICUrl,IsHomePageShow,Taxis,UpdateDate)");
            strSql.Append(" values (");
            strSql.Append("@lmID,@TName,@TContent,@RecordDate,@PICUrl,@IsHomePageShow,@Taxis,@UpdateDate)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@lmID", OleDbType.Integer,4),
					new OleDbParameter("@TName", OleDbType.VarChar,50),
					new OleDbParameter("@TContent", OleDbType.VarChar,0),
					new OleDbParameter("@RecordDate", OleDbType.Date),
					new OleDbParameter("@PICUrl", OleDbType.VarChar,255),
					new OleDbParameter("@IsHomePageShow", OleDbType.Boolean,1),
					new OleDbParameter("@Taxis", OleDbType.Integer,4),
					new OleDbParameter("@UpdateDate", OleDbType.Date)};
            parameters[0].Value = model.lmID;
            parameters[1].Value = model.TName;
            parameters[2].Value = model.TContent;
            parameters[3].Value = model.RecordDate;
            parameters[4].Value = model.PICUrl;
            parameters[5].Value = model.IsHomePageShow;
            parameters[6].Value = model.Taxis;
            parameters[7].Value = model.UpdateDate;

            int rows = OledbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(KeMei.Model.ProductAndCase model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ProductAndCase set ");
            strSql.Append("lmID=@lmID,");
            strSql.Append("TName=@TName,");
            strSql.Append("TContent=@TContent,");
            strSql.Append("PICUrl=@PICUrl,");
            strSql.Append("IsHomePageShow=@IsHomePageShow,");
            strSql.Append("UpdateDate=@UpdateDate");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@lmID", OleDbType.Integer,4),
					new OleDbParameter("@TName", OleDbType.VarChar,50),
					new OleDbParameter("@TContent", OleDbType.VarChar,0),
					new OleDbParameter("@PICUrl", OleDbType.VarChar,255),
					new OleDbParameter("@IsHomePageShow", OleDbType.Boolean,1),
					new OleDbParameter("@UpdateDate", OleDbType.Date),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = model.lmID;
            parameters[1].Value = model.TName;
            parameters[2].Value = model.TContent;
            parameters[3].Value = model.PICUrl;
            parameters[4].Value = model.IsHomePageShow;
            parameters[5].Value = model.UpdateDate;
            parameters[6].Value = model.ID;

            int rows = OledbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public bool SetPicUrl(int ID, string value)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ProductAndCase set ");
            strSql.Append("PICUrl=@PICUrl");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@PICUrl", OleDbType.VarChar,255),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = value;
            parameters[1].Value = ID;

            int rows = OledbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProductAndCase ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            int rows = OledbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProductAndCase ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = OledbHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public KeMei.Model.ProductAndCase GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,lmID,TName,TContent,RecordDate,PICUrl,IsHomePageShow,Taxis,UpdateDate from ProductAndCase ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            KeMei.Model.ProductAndCase model = new KeMei.Model.ProductAndCase();
            DataSet ds = OledbHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["lmID"] != null && ds.Tables[0].Rows[0]["lmID"].ToString() != "")
                {
                    model.lmID = int.Parse(ds.Tables[0].Rows[0]["lmID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TName"] != null && ds.Tables[0].Rows[0]["TName"].ToString() != "")
                {
                    model.TName = ds.Tables[0].Rows[0]["TName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TContent"] != null && ds.Tables[0].Rows[0]["TContent"].ToString() != "")
                {
                    model.TContent = ds.Tables[0].Rows[0]["TContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RecordDate"] != null && ds.Tables[0].Rows[0]["RecordDate"].ToString() != "")
                {
                    model.RecordDate = DateTime.Parse(ds.Tables[0].Rows[0]["RecordDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PICUrl"] != null && ds.Tables[0].Rows[0]["PICUrl"].ToString() != "")
                {
                    model.PICUrl = ds.Tables[0].Rows[0]["PICUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsHomePageShow"] != null && ds.Tables[0].Rows[0]["IsHomePageShow"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsHomePageShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsHomePageShow"].ToString().ToLower() == "true"))
                    {
                        model.IsHomePageShow = true;
                    }
                    else
                    {
                        model.IsHomePageShow = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Taxis"] != null && ds.Tables[0].Rows[0]["Taxis"].ToString() != "")
                {
                    model.Taxis = int.Parse(ds.Tables[0].Rows[0]["Taxis"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdateDate"] != null && ds.Tables[0].Rows[0]["UpdateDate"].ToString() != "")
                {
                    model.UpdateDate = DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,lmID,TName,TContent,RecordDate,PICUrl,IsHomePageShow,Taxis,UpdateDate ");
            strSql.Append(" FROM ProductAndCase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OledbHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ProductAndCase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = OledbHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from ProductAndCase T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return OledbHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "ProductAndCase";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return OledbHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        /// <summary>
        /// 获得最新几条数据列表
        /// yd 2012-05-17
        /// </summary>
        /// <param name="top">条</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public DataSet GetTopDataByIsShow(int top, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select top {0} ID,lmID,TName,TContent,RecordDate,PICUrl,IsHomePageShow,Taxis,UpdateDate  ", top);
            strSql.Append(" FROM ProductAndCase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OledbHelper.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

